-- xmake.lua - 三维重建服务构建配置

-- 设置项目信息
set_project("ReconstructionService")
set_version("1.0.0")
set_description("实时三维重建服务 - C++ 版本")

-- 设置语言标准
set_languages("c++17")

-- 设置构建模式
add_rules("mode.debug", "mode.release")

-- 设置目标平台
set_plat("linux")
set_arch("x64")

-- 添加编译选项
add_cxxflags("-O3", "-march=native", "-ffast-math", "-fopenmp")
add_cxxflags("-Wall", "-Wextra", "-Wpedantic")

-- 添加链接选项
add_ldflags("-fopenmp")

-- 定义目标
target("reconstruction_service")
    set_kind("binary")
    set_default(true)
    
    -- 添加源文件
    add_files("src/*.cpp")
    add_includedirs("include")
    
    -- 添加依赖包
    add_packages("opencv", "nlohmann_json", "cpp-httplib")
    
    -- 设置输出目录
    set_targetdir("build")
    
    -- 添加安装规则
    add_installfiles("build/reconstruction_service", {prefixdir = "bin"})

-- 配置包管理器
package("opencv")
    set_homepage("https://opencv.org")
    set_description("Open Source Computer Vision Library")
    set_license("Apache-2.0")
    
    add_urls("https://github.com/opencv/opencv/archive/$(version).zip")
    add_versions("4.8.1", "sha256")
    
    add_deps("cmake")
    
    on_install("linux", function (package)
        local configs = {}
        table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release"))
        table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
        table.insert(configs, "-DBUILD_EXAMPLES=OFF")
        table.insert(configs, "-DBUILD_TESTS=OFF")
        table.insert(configs, "-DBUILD_PERF_TESTS=OFF")
        table.insert(configs, "-DWITH_CUDA=OFF")
        table.insert(configs, "-DWITH_OPENCL=OFF")
        table.insert(configs, "-DWITH_IPP=OFF")
        table.insert(configs, "-DWITH_TBB=ON")
        table.insert(configs, "-DWITH_OPENMP=ON")
        
        import("package.tools.cmake").install(package, configs)
    end)
    
    on_test(function (package)
        assert(package:has_cfuncs("cvCreateImage", {includes = "opencv2/opencv.hpp"}))
    end)

package("nlohmann_json")
    set_homepage("https://github.com/nlohmann/json")
    set_description("JSON for Modern C++")
    set_license("MIT")
    
    add_urls("https://github.com/nlohmann/json/releases/download/v$(version)/json.tar.xz")
    add_versions("3.11.2", "sha256")
    
    add_deps("cmake")
    
    on_install("linux", function (package)
        local configs = {}
        table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release"))
        table.insert(configs, "-DJSON_BuildTests=OFF")
        
        import("package.tools.cmake").install(package, configs)
    end)
    
    on_test(function (package)
        assert(package:check_cxxsnippets({test = [[
            #include <nlohmann/json.hpp>
            void test() {
                nlohmann::json j = {{"key", "value"}};
            }
        ]]}, {configs = {languages = "c++17"}}))
    end)

package("cpp-httplib")
    set_homepage("https://github.com/yhirose/cpp-httplib")
    set_description("A C++ header-only HTTP/HTTPS server and client library")
    set_license("MIT")
    
    add_urls("https://github.com/yhirose/cpp-httplib/archive/refs/tags/v$(version).tar.gz")
    add_versions("0.14.1", "sha256")
    
    add_deps("cmake")
    
    on_install("linux", function (package)
        local configs = {}
        table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release"))
        table.insert(configs, "-DCPPHTTPLIB_BUILD_TESTS=OFF")
        table.insert(configs, "-DCPPHTTPLIB_BUILD_EXAMPLES=OFF")
        
        import("package.tools.cmake").install(package, configs)
    end)
    
    on_test(function (package)
        assert(package:check_cxxsnippets({test = [[
            #include <httplib.h>
            void test() {
                httplib::Client cli("localhost", 8080);
            }
        ]]}, {configs = {languages = "c++17"}}))
    end) 